#    Copyright 2010, 2011, 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopip/djangoproject/include', '/opt/coopip/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPIP_Ajax, COOPIP_Misc, COOPIP_DynamicTemplate, COOPIP_Form, COOPIP_FormCISReport
import COOPIP_Authentication
from djangoproject.models import *
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms
import AMR, CIS, GIS
import COOPIP_Form, COOPIP_FormGISReport, COOPIP_Misc


from django.db import transaction


def MainMenu(request):
    """MainMenu is the main selection screen for CIS Functions."""
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/gismainmenu.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))
    t = loader.get_template('/opt/coopip/djangoproject/templates/gismainmenu.html')
    c = Context({'LoginStatus': LoginStatus})
    return HttpResponse(t.render(c))


def GetFarStation(Span, Station):
    if(Span.StartStation == Station):
        return Span.EndStation
    else:
        return Span.StartStation

def RebuildGISSymbology(SpanOrStationid):
    os.system('wget http://' + djangoproject.settings.COOPGISServerIPAddress + '/coopgis/rebuildsymbology/' + str(SpanOrStationid) + '/ -O /dev/null')



def GetShortMapNumber(CISLocInfoDetl):
        LongMapNumber=CISLocInfoDetl.location.lstrip('0')
        ShortMapNumber='UNKNOWN'  #Default if it does not match the length criteria
        Length=len(LongMapNumber)
        if(Length < 10 or Length > 11):
           return 'Possible Invalid Location:  ' + str(LongMapNumber)
        if(Length == 10):
            FirstNumber=int(LongMapNumber[0])
            SecondNumber=int(LongMapNumber[1:4])
            ThirdNumber=int(LongMapNumber[4:7])
            FourthNumber=int(LongMapNumber[7:])
            if(ThirdNumber > 99):
                ThirdNumber = str(int(str(ThirdNumber)[0])) + '-' + str(int(str(ThirdNumber)[1:]))
            ShortMapNumber = str(FirstNumber) + '-' + str(SecondNumber) + '-' + str(ThirdNumber) + '-' + str(FourthNumber)
        elif(Length == 11):
            if(int(LongMapNumber[:3]) == 500):
                ENumberIndicator=True
            else:
                ENumberIndicator=False
            if(ENumberIndicator):
                FirstNumber=int(LongMapNumber[3:6])
                SecondNumber=int(LongMapNumber[6:8])
                ThirdNumber=int(LongMapNumber[8:])
                if(SecondNumber):
                    ShortMapNumber = 'E' + str(FirstNumber) + '-' + str(SecondNumber) + '-' + str(ThirdNumber)
                else:
                    ShortMapNumber = 'E' + str(FirstNumber) + '-' + str(ThirdNumber)
            else:
                FirstNumber=int(LongMapNumber[0:2])
                SecondNumber=int(LongMapNumber[2:5])
                ThirdNumber=int(LongMapNumber[5:8])
                FourthNumber=int(LongMapNumber[8:])
                if(ThirdNumber > 99):
                    ThirdNumber = str(int(str(ThirdNumber)[0])) + '-' + str(int(str(ThirdNumber)[1:]))
                ShortMapNumber = str(FirstNumber) + '-' + str(SecondNumber) + '-' + str(ThirdNumber) + '-' + str(FourthNumber)
        return ShortMapNumber

@transaction.commit_on_success
def FixMovedGISMeters(request):
    import django.db
    GenericReportTitle='COOPGIS CIS Report Zero Unmatched GIS Meters'
    GenericMessage='You do not have permission to this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))
    Temp=[]
    #Now zero out duplicate GIS Meters
    #Get a list of unique serialnumbers:
    SerialNumbers = list(GIS.Coopgisassembly.objects.filter(compatibleunit__meterindicator=True).exclude(meterdcsinumber=0).values_list('meterdcsinumber', flat=True).distinct())
    for i in SerialNumbers:
        try:
            IntegerTest = int(i) #handle blank, IDLE, etc
        except ValueError:
            continue
        DuplicateMeters = GIS.Coopgisassembly.objects.filter(compatibleunit__meterindicator=True).filter(meterdcsinumber=i)
        if DuplicateMeters.count() <= 1:
            continue
        TotalLocationCount=0
        for meter in DuplicateMeters:
            LocationString=COOPIP_Misc.GetLongMapNumber(meter).rjust(16,'0')
            CISLocation = CIS.Locationdetl.objects.filter(location=LocationString).filter(discdate='000000')
            TotalLocationCount+=CISLocation.count()
        for meter in DuplicateMeters:
            LocationString=COOPIP_Misc.GetLongMapNumber(meter).rjust(16,'0')
            CISLocation = CIS.Locationdetl.objects.filter(location=LocationString).filter(discdate='000000')
            #if meter can't find a CIS location for itself, movement may have taken place
            #before possibly zeroing all meters, make sure at least one matched up with a CISLocation
            if TotalLocationCount > 1:
                pass
                #Temp.append('Assemblies have identical oldmapnumbers; unable to differentiate DCSI #' + i)
            elif TotalLocationCount == 1 and CISLocation.count():
                Temp.append('GIS Assembly unchanged: Meter#' + str(i) + ' Look for \"set to 0\" on nearby line')
            elif TotalLocationCount == 1 and not CISLocation.count():
                Temp.append('Updated GIS Assembly due to lack of CIS location: Meter #' + str(i) + ' set to 0 at location ' + LocationString)
                meter.meterdcsinumber=0
                meter.meternumber=0
                meter.save()
            else:
                Temp.append('Update cancelled for Meter #' + str(meter.meterdcsinumber) + ' at location ' + LocationString + '; no matching CIS location with active meter')
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))


@transaction.commit_on_success
def FixUnmatchedGISLocations(request):
    """This report shows a list of meter locations that do not match between the CIS and GIS
       It also UPDATES meter numbers in the GIS for which there are matches, based on the meter number in the CIS
       It also UPDATES MeterLocationFromCIS
       It also shows a list of duplicated meters in the GIS that match CIS meter numbers"""
    import django.db
    GenericReportTitle='COOPGIS CIS Report Unmatched GIS Locations'
    GenericMessage='You do not have permission to this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus': LoginStatus, 'Permission': Permission})
        return HttpResponse(t.render(c))
    Temp=[]
    #Perform Data Gathering Tasks
    #Also get a list of GIS locations for which more then 1 have the same map number
    DuplicateGISLocations=[]

    #First, Show GIS Meters that do not have location numbers:
    Temp.append('Meters Lacking Location Numbers:')
    Temp.append('Station'.ljust(20) + 'Assembly ID'.ljust(20) + 'InstallStatus'.ljust(20))
    Meters=GIS.Coopgisassembly.objects.filter(compatibleunit__meterindicator=True).order_by('installstatus_id', 'id')
    for i in Meters:
        if(not i.oldmapnumber):
            Temp.append(str(i.station.id).ljust(20) + str(i.id).ljust(20) + str(i.installstatus_id).ljust(20))
    Temp.append('')
    Temp.append('')
    #Now Show GIS Meters for which matches cannot be found in the CIS
    GISWithInvalidMapNumber=[]
    Meters=GIS.Coopgisassembly.objects.filter(compatibleunit__meterindicator=True).exclude(idleindicator=True).order_by('installstatus_id', 'id')
    Temp.append('GIS Meters which do not have matches in the CIS')
    Temp.append('Station'.ljust(20) + 'Mapnumber'.ljust(20) + 'InstallStatus'.ljust(20))
    for i in Meters:
        if(len(i.oldmapnumber) < 3):
            GISWithInvalidMapNumber.append(i)
            continue
        TempDebug=i.id
        LongMapNumber=COOPIP_Misc.GetLongMapNumber(i)
        NumberOfMatches=CIS.Locinfodetl.objects.filter(location=LongMapNumber.rjust(16, '0')).count()
        if(NumberOfMatches==0):
            #check for single CIS account match
            if not CIS.Memberdetl.objects.filter(location=LongMapNumber.rjust(16, '0')).count() == 1:
                Temp.append(str(i.station.id).ljust(20) + i.oldmapnumber.ljust(20) + str(i.installstatus_id).ljust(20))
        elif(NumberOfMatches > 1):
            Temp.append('The CIS has multiple matches for:  ' + i.oldmapnumber)
        else:  #NumberOfMatches == 1, and there is a clear GIS record, so update it as long as there are not duplicate Map Numbers in the GIS for this meter
            CheckForDuplicates=GIS.Coopgisassembly.objects.filter(oldmapnumber=i.oldmapnumber)
            if(not CheckForDuplicates.count() == 1):
                continue
            try:
                CISMeter=CIS.Meterdetl.objects.filter(location=LongMapNumber.rjust(16, '0')).filter(idlecode='00').get()
            except:  #If there were no non-idle meters in the CIS at this map number (location), then set the meter number to 0 in the GIS
                if not (i.meternumber in[0, '0']) or not (i.meterdcsinumber in [0,'0']):
                    Temp.append('Updated GIS Meter Number for Map Number:  ' + i.oldmapnumber + ' Old Meter Number:  ' + i.meternumber + '  NewMeterNumber:  ' + '0'  )
                    Temp.append('Updated GIS Meter DCSI Number for Map Number:  ' + i.oldmapnumber+ ' Old Meter DCSINumber:  ' + i.meterdcsinumber + '  NewMeterDCSINumber:  ' + '0' )
                    i.meternumber = 0
                    i.meterdcsinumber = 0
                    i.save()
                continue
            CISMeterDCSINumber=CISMeter.serial.rstrip()  #Pulling out here for reference, not actually used in the CIS
            CISMeterNumber=CISMeter.meter.lstrip('0') #Replacing both the meter and the dcsi number with this number 
            if(not i.meternumber == CISMeterNumber):
                OldMeterNumber=i.meternumber
                i.meternumber=CISMeterNumber
                i.save()
                RebuildGISSymbology(i.station.id)
                Temp.append('Updated GIS Meter Number for Map Number:  ' + i.oldmapnumber + ' Old Meter Number:  ' + OldMeterNumber + '  NewMeterNumber:  ' + i.meternumber  )
            if(not i.meterdcsinumber == CISMeterNumber):
                OldDCSINumber = i.meterdcsinumber
                i.meterdcsinumber=CISMeterNumber
                i.save()
                RebuildGISSymbology(i.station.id)
                Temp.append('Updated GIS Meter DCSI Number for Map Number:  ' + i.oldmapnumber+ ' Old Meter DCSINumber:  ' + OldDCSINumber + '  NewMeterDCSINumber:  ' + i.meterdcsinumber )
            if(not i.meterlocationnumberfromcis == CISMeter.location):
                OldMeterLocationNumberFromCIS=i.meterlocationnumberfromcis
                i.meterlocationnumberfromcis = CISMeter.location
                i.save()
                Temp.append('Updated GIS MeterLocationNumberFromCIS:  ' + i.oldmapnumber+ ' Old MeterLocationNumberFromCIS:  ' + OldMeterLocationNumberFromCIS + '  NewMeterLocationNumberFromCIS:  ' + i.meterlocationnumberfromcis)
    Temp.append('')
    Temp.append('')
    #Now Show GIS Meters with invalid map numbers
    Temp.append('GIS Meters with Invalid Map Numbers')
    Temp.append('Station'.ljust(20) + 'Mapnumber'.ljust(20) + 'InstallStatus'.ljust(20))
    for i in GISWithInvalidMapNumber:
        Temp.append(str(i.station.id).ljust(20) + i.oldmapnumber.ljust(20) + str(i.installstatus_id).ljust(20))
    Temp.append('')
    Temp.append('')
    #Now Show CIS Locations for which matches cannot be found in the GIS
    Temp.append('CIS Meters with no GIS Matches')
    Temp.append('CIS Mapnumber'.ljust(20) + 'MapNumber'.ljust(20) + 'Pole'.ljust(40) + 'Meter Number'.ljust(20) + 'Comments'.ljust(30))
    CISLocations=CIS.Locinfodetl.objects.all().exclude(idlecode='01').order_by('location')
    UnmatchedCISLocations=[]
    for i in CISLocations:
        try:
            locationdetl=CIS.Locationdetl.objects.filter(location=i.location).order_by('discdate')[0]
            if(locationdetl.discdate == '000000'):
                meternumber=locationdetl.meter
            else:
                meternumber='disconnected'
        except: 
            meternumber='unknown'
        ShortMapNumber=GetShortMapNumber(i)
        NumberOfMatches=GIS.Coopgisassembly.objects.filter(oldmapnumber=ShortMapNumber).count()
        if(NumberOfMatches == 0):
            Temp.append(i.location.ljust(20) + ShortMapNumber.ljust(20) + str(i.pole.ljust(40)) + str(meternumber).ljust(20) + str(i.comments).ljust(30))
            UnmatchedCISLocations.append(i)
        elif(NumberOfMatches > 1):
            DuplicateGISLocations.append(i)
    Temp.append('')
    Temp.append('')
    #Now Show the Duplicated Map Numbers in the GIS
    Temp.append('Duplicated GIS Locations')
    Temp.append('Station'.ljust(20) + 'MapNumber'.ljust(20) + 'Installstatus'.ljust(20))
    for i in DuplicateGISLocations:
        ShortMapNumber=GetShortMapNumber(i)
        Assemblies=GIS.Coopgisassembly.objects.filter(oldmapnumber=ShortMapNumber).order_by('installstatus_id', 'id')
        for assembly in Assemblies:
            Temp.append(str(assembly.station.id).ljust(20) + ShortMapNumber.ljust(20) + str(assembly.installstatus_id).ljust(20))
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))


def AMRPhaseUpdateNoChanges(request):
    return AMRPhaseUpdate(request, NoChanges=True)



def GetPhaseCode(assembly, GetNeutral=False):
    """Returns the ABC Phase code for an assembly"""
    String=''
    if(assembly.phaseaindicator):
        String = String + 'A'
    if(assembly.phasebindicator):
        String = String + 'B'
    if(assembly.phasecindicator):
        String = String + 'C'
    if(GetNeutral and assembly.phaseneutralindicator):
        String = String + 'N'
    return String


def AMRPhaseUpdate(request, NoChanges=False):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='AMRPhaseUpdate'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]
    Meters = GIS.Coopgisassembly.objects.filter(compatibleunit__meterindicator=True).exclude(meterdcsinumber__iexact = 'IDLE').exclude(meterdcsinumber = '').order_by('circuitnumber', 'meterdcsinumber')

    Temp.append('Meters: ' + str(Meters.count()))
    #Setup multiple lists, meters changed, meters ignored due to multiple phases in GIS, meters not found etc
    #put headings over each list, then list the details
    MetersChanged = []   
    MetersIgnored = []
    MetersNotFound = []
    for meter in Meters:
        #Check how many phases the GIS meter has, if it has 2 or 3, then just print it out with the phases under a heading not changed due to number of phases
        try:
            decimal.Decimal(meter.meterdcsinumber)
            validMeterIndicator = True
        except:
            validMeterIndicator = False
        if (meter.phaseaindicator and meter.phasebindicator) or (meter.phasebindicator and meter.phasecindicator) or (meter.phasecindicator and meter.phaseaindicator): 
            MetersIgnored.append(meter)
        elif not validMeterIndicator:
            MetersIgnored.append(meter)
        else:
            AMRMeters = AMR.Serialnumber.objects.all().filter(serialnumber = decimal.Decimal(meter.meterdcsinumber))
            if AMRMeters.count() == 1:
                if not(GetPhaseCode(meter)):
                    meterString = 'None'
                else:
                    meterString = GetPhaseCode(meter)
                if AMRMeters[0].phase == 1 and meter.phaseaindicator == False:  #and if the meter phase is not already A, then change it...  Print a message of the original GIS phase and the new phase
                    MetersChanged.append(meter.meterdcsinumber.ljust(20) + 'Changed from Phase ' + meterString + ' to Phase A     Circuit:  ' + str(meter.circuitnumber))
                    if(not NoChanges):
                        meter.phaseaindicator = True
                        meter.phasebindicator = False
                        meter.phasecindicator = False
                        meter.save()
                        RebuildGISSymbology(meter.station.id)
	        elif AMRMeters[0].phase == 2 and meter.phasebindicator == False:
                    MetersChanged.append(meter.meterdcsinumber.ljust(20) + 'Changed from Phase ' + meterString + ' to Phase B     Circuit:  ' + str(meter.circuitnumber))
                    if(not NoChanges):
                        meter.phaseaindicator = False
                        meter.phasebindicator = True
                        meter.phasecindicator = False
                        meter.save()
                        RebuildGISSymbology(meter.station.id)
                elif AMRMeters[0].phase == 3 and meter.phasecindicator == False:
                    MetersChanged.append(meter.meterdcsinumber.ljust(20) + 'Changed from Phase ' + meterString + ' to Phase C     Circuit:  ' + str(meter.circuitnumber))
                    if(not NoChanges):
                        meter.phaseaindicator = False
                        meter.phasebindicator = False
                        meter.phasecindicator = True
                        meter.save()
                        RebuildGISSymbology(meter.station.id)
                else:
                    pass
            elif not AMRMeters.count():
                MetersNotFound.append(meter.meterdcsinumber.ljust(20) + 'Could Not Find in AMR')
            else:
                MetersNotFound.append(meter.meterdcsinumber.ljust(20) + 'Multiple Matches Found on AMR')

    spacer = '\n\n'
    if(NoChanges):
        Temp.append('METERS THAT WOULD BE CHANGED IF CHANGES WERE ENABLED')
    else:
        Temp.append('METERS CHANGED')

    for entry in MetersChanged:
        Temp.append(entry)
    Temp.append(spacer + 'METERS IGNORED: MULTIPLE GIS PHASES'.ljust(40) + 'Phase A'.ljust(20) + 'Phase B'.ljust(20) + 'Phase C'.ljust(20))
    for meter in MetersIgnored:
        Temp.append(meter.meterdcsinumber.ljust(40) + str(meter.phaseaindicator).ljust(20) + str(meter.phasebindicator).ljust(20) + str(meter.phasecindicator).ljust(20))
    Temp.append(spacer + 'METERS NOT FOUND: DUPLICATE ENTRIES OR NO MATCH IN AMR ')
    for entry in  MetersNotFound:
        Temp.append(entry)

    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))



def UpdateCircuitNamesFromAMR(request):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='AMRGISUpdateCircuitNames'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    TempList=[]
    AMRFeeders=list(AMR.Feeder.objects.all().order_by('subid', 'busid', 'fdrid'))
    GISSubstationCircuits=list(GIS.Substationcircuit.objects.all().order_by('substation', 'circuitnumber', 'name'))
    GenericMessage=''

    for aMRFeeder in AMRFeeders:
        GISMatch=GIS.Substationcircuit.objects.filter(amrsubstationidentifier = aMRFeeder.subid).filter(amrbusidentifier = aMRFeeder.busid).filter(amrfeederidentifier = aMRFeeder.fdrid)
        if(GISMatch.count() == 1):
            match = GISMatch[0]
            match.amrcircuitname = aMRFeeder.feedername
            match.save()
        else:
            TempList.append("No Exact GIS Match for AMR Feeder:  " + str(aMRFeeder.feedername) + '   ' + str(aMRFeeder.subid) + '   ' + str(aMRFeeder.busid) + '    ' + str(aMRFeeder.fdrid) + '    GIS Matches:  ' + str(GISMatch.count()))
        
        
    for gisSubstationCircuit in GISSubstationCircuits:
        AMRMatch=AMR.Feeder.objects.filter(subid = gisSubstationCircuit.amrsubstationidentifier).filter(busid = gisSubstationCircuit.amrbusidentifier).filter(fdrid = gisSubstationCircuit.amrfeederidentifier)
        if(AMRMatch.count() == 1):
            pass
        else:
            TempList.append("No Exact AMR Match for GIS Circuit:  " + str(gisSubstationCircuit.name) + '  ' + str(gisSubstationCircuit.circuitnumber) + '  ' + str(gisSubstationCircuit.amrcircuitname) + '   ' +  str(gisSubstationCircuit.amrsubstationidentifier) + \
                             '   ' + str(gisSubstationCircuit.amrbusidentifier) + '    ' + str(gisSubstationCircuit.amrfeederidentifier) + '    AMR Matches:  ' + str(AMRMatch.count()))
 
                    
    Preformatted = '\n'.join(TempList)    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))



def AMRGISUnmatchedCircuits(request):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='AMRGISUnmatchedCircuitsUpdate'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]
    Meters = GIS.Coopgisassembly.objects.all().filter(compatibleunit__meterindicator = True).exclude(meterdcsinumber__iexact = 'IDLE'\
                      ).exclude(meterdcsinumber = '').order_by('circuitnumber', 'meterdcsinumber')

    AMRMeters=list(AMR.Serialnumber.objects.all())
    AMRMeterDict={}
    for i in AMRMeters:
        AMRMeterDict[str(i.serialnumber)] = i

    Temp.append('Meters: ' + str(Meters.count()))
    Matches=0
    UnmatchedMessages=[]
    NoMatchMessages=[]
    UnmatchedMessages.append(''.ljust(30) + 'Station'.ljust(10) + 'DCSI #'.ljust(20) + 'GIS Circuit #'.ljust(20) + 'AMR Sub'.ljust(10) + 'AMR Bus'.ljust(10) + 'AMR Fdr'.ljust(10) + 'NewGISCircuit'.ljust(15))
    NoChangeList=[]
    #FIXME need to move this code somewhere else
    #for station in GIS.Coopgisstation.objects.all():
    #    station.compatibleunitonename='Unmatched Report'
    #    station.compatibleunitonenumber=0
    #    station.compatibleunittwoname='UnmatchedReport'
    #    station.compatibleunittwonumber=0
    #    station.save()
    for meter in Meters:
        try:
            MeterDCSINumber=int(meter.meterdcsinumber)
            if(not MeterDCSINumber):
                continue
        except:
            continue
        try:
            AMRMeter=AMRMeterDict[str(MeterDCSINumber)]
        except:
            NoMatchMessages.append('No Matches Found For:  '.ljust(30) + str(meter.meterdcsinumber).ljust(20) + str(meter.circuitnumber).ljust(20))
            continue
        if(meter.oldmapnumber.strip() in djangoproject.settings.AMRMeterCircuitUpdateMapNumbersToSkip):
            continue
        GISCircuit=GIS.Substationcircuit.objects.filter(circuitnumber=meter.circuitnumber)
        if(not len(GISCircuit) == 1):
            continue
        GISCircuit=GISCircuit[0]
        if(AMRMeter.subid == GISCircuit.amrsubstationidentifier and AMRMeter.busid == GISCircuit.amrbusidentifier and
           AMRMeter.fdrid == GISCircuit.amrfeederidentifier):
            Matches = Matches + 1
            NoChangeList.append('No Change Made to:  '.ljust(30) + str(meter.station.id).ljust(10) + str(meter.meterdcsinumber).ljust(20) + str(meter.circuitnumber).ljust(20) +  str(AMRMeter.subid).ljust(10) + \
                     str(AMRMeter.busid).ljust(10) + str(AMRMeter.fdrid).ljust(10) + str(meter.circuitnumber).ljust(15) + str(GISCircuit.amrcircuitname))
            continue
        GISCircuits=GIS.Substationcircuit.objects.filter(amrsubstationidentifier = AMRMeter.subid).filter(amrbusidentifier=AMRMeter.busid).filter(amrfeederidentifier=AMRMeter.fdrid)
        if(len(GISCircuits) == 1):
            ProposedGISCircuitNumber=GISCircuits[0].circuitnumber
        else:
            ProposedGISCircuitNumber='Unknown'
        UnmatchedMessages.append('Unmatched Meter Found:  '.ljust(30) + str(meter.station.id).ljust(10) + str(meter.meterdcsinumber).ljust(20) + str(meter.circuitnumber).ljust(20) +  str(AMRMeter.subid).ljust(10) + \
                     str(AMRMeter.busid).ljust(10) + str(AMRMeter.fdrid).ljust(10) + str(ProposedGISCircuitNumber).ljust(15) + str(GISCircuit.amrcircuitname))
        if(not ProposedGISCircuitNumber == 'Unknown'):
            meter.circuitnumber=ProposedGISCircuitNumber
            meter.save()
            RebuildGISSymbology(meter.station.id)
                
    Preformatted='Matched Meters Found:  ' + str(Matches) + '\n' + '\n'.join(UnmatchedMessages) + '\n' +  '\n'.join(NoMatchMessages) + '\n' + '\n'.join(NoChangeList)
    #End Data Gathering Tasks
    GenericMessage=''
    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))


def GetTransformerOrSecondaryWire(station):
    Transformers=list(GIS.Coopgisassembly.objects.all().filter(compatibleunit__transformerindicator = True).filter(station=station))
    if(Transformers):
        return Transformers[0]
    Spans=list(GIS.Coopgisspan.objects.filter(startstation=station))
    Spans2=GIS.Coopgisspan.objects.filter(endstation=station)
    for span in Spans2:
        Spans.append(span)
    for span in Spans:
        SecondaryWire=list(GIS.Coopgisassembly.objects.all().filter(compatibleunit__conductorindicator=True).exclude(primaryindicator=True).filter(span=span))
        if(SecondaryWire):
            return SecondaryWire[0]
    return None


def IsAssemblySinglePhase(assembly):
    Count = 0
    if(assembly.phaseaindicator):
        Count = Count + 1
    if(assembly.phasebindicator):
        Count = Count + 1
    if(assembly.phasecindicator):
        Count = Count + 1
    return bool(Count == 1)



def GISUndoAMRUnmatchedCircuits(request):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='GISUndoAMRUnmatchedCircuits'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]
    Meters = list(GIS.Coopgisassembly.objects.all().filter(compatibleunit__meterindicator = True).exclude(meterdcsinumber__iexact = 'IDLE'\
                      ).exclude(meterdcsinumber = '').order_by('circuitnumber', 'meterdcsinumber'))

    for meter in Meters:
        assembly = GetTransformerOrSecondaryWire(meter.station)
        if(not assembly):
            Temp.append('No Assembly for:  ' + str(meter.station.id))
            continue
        if(assembly.circuitnumber != meter.circuitnumber):
            Temp.append('Changing Station:  ' + str(meter.station.id).ljust(15) + str(meter.meterdcsinumber).ljust(20) + ' Circuit from:  ' + str(meter.circuitnumber).ljust(20)  + '  to:  ' + str(assembly.circuitnumber).ljust(20))
            meter.circuitnumber = assembly.circuitnumber
            meter.save()
            RebuildGISSymbology(meter.station.id)

        if(IsAssemblySinglePhase(meter) and IsAssemblySinglePhase(assembly)):
            if(meter.phaseaindicator == assembly.phaseaindicator) and (meter.phasebindicator == assembly.phasebindicator) and (meter.phasecindicator == assembly.phasecindicator):
                continue
            meter.phaseaindicator = assembly.phaseaindicator
            meter.phasebindicator = assembly.phasebindicator
            meter.phasecindicator = assembly.phasecindicator
            Temp.append('Changing Station:  ' + str(meter.station.id).ljust(15) + str(meter.meterdcsinumber).ljust(20) + ' Phase Adjustment')

            meter.save()
            RebuildGISSymbology(meter.station.id)

                
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))











def GISUndoAMRUnmatchedCircuitsNoChanges(request):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='GISUndoAMRUnmatchedCircuitsReportOnly'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]
    Meters = list(GIS.Coopgisassembly.objects.all().filter(compatibleunit__meterindicator = True).exclude(meterdcsinumber__iexact = 'IDLE'\
                      ).exclude(meterdcsinumber = '').order_by('circuitnumber', 'meterdcsinumber'))

    for meter in Meters:
        assembly = GetTransformerOrSecondaryWire(meter.station)
        if(not assembly):
            Temp.append('No Assembly for:  ' + str(meter.station.id))
            continue
        if(assembly.circuitnumber != meter.circuitnumber):
            try:
                coopipmeter=Meter.objects.filter(SerialNumber=meter.meterdcsinumber)[0]
                if(coopipmeter.AMRSubstationName):
                    Substation = ':' + coopipmeter.AMRSubstationName
                else:
                    Substation=''
            except IndexError:
                Substation = ''
            if(meter.meterdcsinumber):
                Temp.append(meter.meterdcsinumber + Substation)

    Temp.sort()
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))













def GISReportAMRUnmatchedCircuits(request):
    import django.db
    import AMR
    import decimal
    GenericReportTitle='GISReportAMRUnmatchedCircuits'
    GenericMessage='You do not have permission to access this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]



    AMRFeeders=list(AMR.Feeder.objects.all().order_by('subid', 'busid', 'fdrid'))
    GISSubstationCircuits=list(GIS.Substationcircuit.objects.all().order_by('substation', 'circuitnumber', 'name'))
    GenericMessage=''
    AMRFeederNameDict={}
    for aMRFeeder in AMRFeeders:
        GISMatch=GIS.Substationcircuit.objects.filter(amrsubstationidentifier = aMRFeeder.subid).filter(amrbusidentifier = aMRFeeder.busid).filter(amrfeederidentifier = aMRFeeder.fdrid)
        if(GISMatch.count() == 1):
            AMRFeederNameDict[GISMatch[0].circuitnumber]=aMRFeeder.feedername
                
        








    Meters = list(GIS.Coopgisassembly.objects.all().filter(compatibleunit__meterindicator = True).exclude(meterdcsinumber__iexact = 'IDLE'\
                      ).exclude(meterdcsinumber = '').order_by('circuitnumber', 'meterdcsinumber'))

    for meter in Meters:
        assembly = GetTransformerOrSecondaryWire(meter.station)
        if(not assembly):
            Temp.append('No Assembly for:  ' + str(meter.station.id))
            continue
        if(assembly.circuitnumber != meter.circuitnumber):
            try:
                Temp.append(str(meter.meterdcsinumber).ljust(20) + ' AMR Circuit:  ' + str(AMRFeederNameDict[meter.circuitnumber]).ljust(20)  + '  GIS Circuit:  ' + str(AMRFeederNameDict[assembly.circuitnumber]).ljust(20))
            except KeyError:
                Temp.append(str(meter.meterdcsinumber).ljust(20) + ' AMR Circuit:  ' + str(meter.circuitnumber).ljust(20)  + '  GIS Circuit:  ' + str(assembly.circuitnumber).ljust(20))
        
        if(IsAssemblySinglePhase(meter) and IsAssemblySinglePhase(assembly)):
            if(meter.phaseaindicator == assembly.phaseaindicator) and (meter.phasebindicator == assembly.phasebindicator) and (meter.phasecindicator == assembly.phasecindicator):
                continue
            #Temp.append('MisMatched Meter Phase:  ' + str(meter.station.id).ljust(15) + str(meter.meterdcsinumber).ljust(20) + ' Phase Adjustment')
            


                
    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))




def GetSubstation(substationEquipment):
    Assignments=GIS.Substationequipmentsubstationassignment.objects.filter(substationequipment_id=substationEquipment.id).order_by('-datetime')
    if(Assignments.count()):
        return Assignments[0].substation
    else:
        return None

def GetSubstationCircuitAssignment(substationEquipment):
    Assignments=GIS.Substationequipmentcircuitassignment.objects.filter(substationequipment_id=substationEquipment.id).order_by('-datetime')
    if(Assignments.count()):
        return GIS.Substationcircuit.objects.filter(id=Assignments[0].substationcircuit_id)[0]
    else:
        return None

def SubstationPeakAmps(request):
    """SubstationPeakAmps is in COOPIP so that future versions can pull other substation reading data, such as from AMR or SCADA as it becomes available"""
    import django.db
    GenericReportTitle='COOPIP Substation Report Peak Amps'
    GenericMessage='You do not have permission to this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]
    import datetime

    RecloserModels=GIS.Substationequipmentmodeltype.objects.filter(recloserindicator=True)
    Reclosers=[]
    for i in RecloserModels:
        MatchingReclosers=GIS.Substationequipment.objects.filter(substationequipmentmodeltype_id = i.id)
        for j in MatchingReclosers:
            Reclosers.append(j)

    Temp.append('id'.ljust(10) + 'Substation'.ljust(20) + 'Circuit'.ljust(40) + 'CreateDateTime'.ljust(30) + 'MaxDemandA'.ljust(20) + 'MaxDemandB'.ljust(20) + 'MaxDemandC'.ljust(20) + 'MaxDemandGround'.ljust(20))
    for recloser in Reclosers:
        SubstationAssignment=GetSubstation(recloser)
        Circuit=GetSubstationCircuitAssignment(recloser)
        Inspections=GIS.Substationequipmentinspection.objects.filter(substationequipment=recloser).order_by('createdatetime')
        for i in Inspections:
            Temp.append(str(recloser.id).ljust(10) + str(SubstationAssignment.name).ljust(20) + str(Circuit.name).ljust(40) + str(i.createdatetime).ljust(30) +  str(i.reclosermaxdemanda).ljust(20) + str(i.reclosermaxdemandb).ljust(20) + str(i.reclosermaxdemandc).ljust(20) + str(i.reclosermaxdemandground).ljust(20))       
    Temp.append('')
    Temp.append('id'.ljust(10) + 'Substation'.ljust(20) + 'Circuit'.ljust(40) + 'CreateDateTime'.ljust(30) + 'Regulator Amp:  '.ljust(20))

    RegulatorModels=GIS.Substationequipmentmodeltype.objects.filter(regulatorindicator=True)
    Regulators=[]
    for i in RegulatorModels:
        MatchingRegulators=GIS.Substationequipment.objects.filter(substationequipmentmodeltype_id = i.id)
        for j in MatchingRegulators:
            Regulators.append(j)

    for regulator in Regulators:
        SubstationAssignment=GetSubstation(regulator)
        Circuit=GetSubstationCircuitAssignment(regulator)
        Inspections=GIS.Substationequipmentinspection.objects.filter(substationequipment=regulator).order_by('createdatetime')
        for i in Inspections:
            try:
                SubstationName=SubstationAssignment.name
            except:
                SubstationName='Unknown'
            try:
                CircuitName=Circuit.name
            except:
                CircuitName='Unknown'
            Temp.append(str(regulator.id).ljust(10) + str(SubstationName).ljust(20) + str(CircuitName).ljust(40) + str(i.createdatetime).ljust(30) +  str(i.regulatormaxamp).ljust(20))
 


    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))



def AMRMetersWithCircuitInfo(request):
    import django.db
    GenericReportTitle='Meters in AMR With Circuit Information'
    GenericMessage='You do not have permission to this application.'
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
        c = Context({'LoginStatus' : LoginStatus, 'GenericReportTitle': GenericReportTitle,
                     'GenericMessage': GenericMessage})
        return HttpResponse(t.render(c))
    Temp=[]

    Temp.append('This information is what AMR is showing for each meter')
    AMRMeter=list(AMR.Serialnumber.objects.all().order_by('subid', 'busid', 'fdrid', 'serialnumber'))
    Feeders=AMR.Feeder.objects.all()
    FeederDict={}
    for i in Feeders:
        SubstationName=AMR.Scemain.objects.filter(subid=i.subid)[0].subname
        FeederDict[str(i.subid) + str(i.busid) + str(i.fdrid)] = SubstationName + ':  ' + i.feedername 

    Temp.append('Sub/Feeder'.ljust(30) + 'DCSINumber'.ljust(20) + 'Sub'.ljust(7) + 'Feeder'.ljust(7))
    for i in AMRMeter:
        try:
            FeederName=FeederDict[str(i.subid) + str(i.busid) + str(i.fdrid)]
        except KeyError:
            FeederName='Unknown'
        Temp.append(FeederName.ljust(30) + str(i.serialnumber).ljust(20) + str(i.subid).ljust(7) + str(i.busid).ljust(7) + str(i.fdrid).ljust(7))

    Preformatted='\n'.join(Temp)
    #End Data Gathering Tasks
    GenericMessage=''
    t = loader.get_template('/opt/coopip/djangoproject/templates/genericreporttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted': Preformatted, 'GenericReportTitle': GenericReportTitle,
                 'GenericMessage': GenericMessage})
    return HttpResponse(t.render(c))



class TableRow():
    pass



def TransformerUsageAnalysisView(request):
    Permission, LoginStatus, Username = COOPIP_Authentication.COOPIPAuthenticate(request, ['COOPIP_Engineering'])
    if(not Permission):
        t = loader.get_template('/opt/coopip/djangoproject/templates/transformerusageanalysis.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPIP_Form.BuildDynamicForm(request, 'TransformerUsageAnalysis')
    TransformerUsageAnalysises=COOPIP_FormGISReport.ProcessTransfomerUsageAnalysisForm(form, UserName=Username)
    form=COOPIP_FormGISReport.PopulateTransformerUsageAnalysisForm(form)
    Count=len(TransformerUsageAnalysises)
    #Analysis=TransformerUsageAnalysis()
    #t = GIS.Coopgisassembly.objects.filter(compatibleunit__transformerindicator=True)[325]
    #Analysis.PerformAnalysis(t, datetime.datetime.now() - datetime.timedelta(days=1))
    #a = TransformerUsageAnalysis.objects.all().order_by('-id')[0]
    #raise(Exception(str(a.StationIdentifier) + '\n' + str(a.MaxUtilizationPercent) + '\n' + str(a.TotalMeterUsage)))



    Rows=[]
    for i in TransformerUsageAnalysises:
        row=TableRow()
        row.Transformers=str(i.TransformerCompatibleUnitNameList)
        row.Station=str(i.StationIdentifier)
        row.Date=str(i.AnalysisDateTime)[:10]
        row.MaxPercent=str(round(i.MaxUtilizationPercent, 2))
        row.AveragePercent=str(round(i.AverageUtilizationPercent, 2))
        row.MaxUsage=str(round(i.MaxUsage, 2))
        row.Missing=str(i.MissingReadingIndicator)
        row.MeterNumberList=i.MeterNumberList
        row.CircuitNumber=i.CircuitNumber
        try:
            station=Station.objects.filter(id=i.StationIdentifier)[0]
            row.StationNote=station.TransformerUsageAnalysisComment
            if(not row.StationNote):
                row.StationNote=''
        except:
            row.StationNote=''
        Rows.append(row)



    #End Data Gathering Tasks
    t = loader.get_template('/opt/coopip/djangoproject/templates/transformerusageanalysis.html')
    c = Context({'LoginStatus' : LoginStatus, 'Rows': Rows, 'form':form, 'Count': Count })
    return HttpResponse(t.render(c))


    










